﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_FactumDocument]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_FactumDocument];
GO
CREATE PROCEDURE [dbo].[sproc_FactumDocument]
    @DocID int,
    @FactumType bit = 0
/*

===========================================================
功能:    呈送文档
参数:
    @DocID int        :    被呈送文档ID
    @FactumType bit = 0    :    呈送类型（0>复制，1>移动）
===========================================================

*/

AS
DECLARE @NewDocID int
DECLARE @ToID int

SET @ToID = 0
--得到被呈送文档的父组
SELECT @ToID = ClassParentID 
    FROM uds_class
    WHERE classid = (SELECT ClassID FROM uds_document 
                WHERE DocID = @DocID)

IF @FactumType  = 0
BEGIN
    --拷贝文档主体
    INSERT INTO uds_document (ClassID,DocTitle,DocContent,DocApprover,DocApproveDate,DocApproved,DocLastViewDate,DocLastViewer,DocViewedTimes,DocType,DocAddedBy,DocAddedDate,DocAttribute,DocDeletion,DocSign)
        SELECT @ToID,DocTitle,DocContent,DocApprover,DocApproveDate,DocApproved,DocLastViewDate,DocLastViewer,DocViewedTimes,DocType,DocAddedBy,DocAddedDate,DocAttribute,DocDeletion,@@spid 
            FROM uds_document
            WHERE DocID = @DocID
    --拷贝文档附件
    SET @NewDocID = @@IDENTITY
    INSERT INTO uds_files (DocID,[FileName],FileSize,FileAttribute,FileVisualPath,FileAuthor,FileCatlog,FileAddedDate)
        SELECT @NewDocID,[FileName],FileSize,FileAttribute,FileVisualPath,FileAuthor,FileCatlog,getdate() 
            FROM uds_files
            WHERE DocID = @DocID
END
ELSE
    --移动文档到上级
    UPDATE uds_document 
        SET ClassID = @ToID
        WHERE DocID = @DocID